package com.meishij.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.meishij.R;
import com.meishij.entry.ShihuaEntry;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by Administrator on 2016/10/13.
 * 我是youngKaka 我的心愿是世界和平
 */

public class ShiHua_HotTopic_Adpater extends RecyclerView.Adapter<ShiHua_HotTopic_Adpater.ViewHolder> {


    private Context context;
    private List<ShihuaEntry.HotTopicBean> list;
    private int dataCount;

    public ShiHua_HotTopic_Adpater(Context context, List<ShihuaEntry.HotTopicBean> list) {
        this.context = context;
        this.list = list;
        dataCount = list.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_shihua_child_hotpic, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.itemView.setOnClickListener(list.get(position));
        holder.hot_username.setText(list.get(position).getUser_info().getUser_name());
        holder.hot_time.setText(list.get(position).getTime() + " 来自 ");
        holder.hot_recipe.setText(list.get(position).getRecipe_info().getTitle());
        holder.hot_ding.setText(list.get(position).getDing_num() + "");
        holder.hot_comment.setText(list.get(position).getComment_num() + "");
        holder.hot_summary.setText(list.get(position).getSummary());
        Uri uri = Uri.parse(list.get(position).getUser_info().getAvatar());
        holder.hot_user_logo.setImageURI(uri);
        holder.gridlayout_hot_topic.removeAllViews();
        int img_num = list.get(position).getImg_num();
        //根据条目的数目 动态设置columnCount
        if (img_num == 4) {
            holder.gridlayout_hot_topic.setColumnCount(2);
        } else if(img_num==8){
            holder.gridlayout_hot_topic.setColumnCount(4);
        } else {
            holder.gridlayout_hot_topic.setColumnCount(3);
        }
        for (int i = 0; i < img_num; i++) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_shihua_child_hotpic_pic, null);
            SimpleDraweeView simpleDraweeView = (SimpleDraweeView) inflate.findViewById(R.id.sdv_shihua_hot_img);
            Uri image_uri = Uri.parse(list.get(position).getImgs().get(i).getSmall());
            simpleDraweeView.setImageURI(image_uri);
            holder.gridlayout_hot_topic.addView(inflate);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {


        TextView hot_summary;
        TextView hot_username;
        TextView hot_time;
        TextView hot_recipe;
        TextView hot_ding;
        TextView hot_comment;
        SimpleDraweeView hot_user_logo;
        GridLayout gridlayout_hot_topic;

        public ViewHolder(View itemView) {
            super(itemView);
            hot_summary = ((TextView) itemView.findViewById(R.id.tv_shihua_hot_summary));
            hot_username = ((TextView) itemView.findViewById(R.id.tv_shihua_hot_username));
            hot_time = ((TextView) itemView.findViewById(R.id.tv_shihua_hot_time));
            hot_recipe = ((TextView) itemView.findViewById(R.id.tv_shihua_hot_recipe));
            hot_ding = ((TextView) itemView.findViewById(R.id.tv_shihua_hot_ding));
            hot_comment = ((TextView) itemView.findViewById(R.id.tv_shihua_hot_comment));
            hot_user_logo = ((SimpleDraweeView) itemView.findViewById(R.id.iv_shihua_hot_userlogo));
            gridlayout_hot_topic = ((GridLayout) itemView.findViewById(R.id.gridlayout_shihua_hot_topic));
        }
    }

    public void removeData() {
        list.clear();
        notifyItemRangeRemoved(0,dataCount);
        dataCount = 0;
    }

    public void addAll(List<ShihuaEntry.HotTopicBean> listbean) {
        int firstpos=list.size();
        list.addAll(listbean);
        dataCount = dataCount + listbean.size();
        notifyItemRangeInserted(firstpos,dataCount);
    }

}
